package com.JavaBean;

import java.sql.*;

public class BookDAO_old {
    private static Connection connection;

    public BookDAO_old() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/javaweb";
            connection = DriverManager.getConnection(url, "Lingqin", "123456");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ResultSet result() throws SQLException {
        Statement stmt = connection.createStatement();
        String sql = "select * from books";
        return stmt.executeQuery(sql);
    }

    public static String bookID(int i) throws SQLException {
        Statement stmt = connection.createStatement();
        String sql = "select * from books";
        ResultSet rs = stmt.executeQuery(sql);

        BookBean book = new BookBean();
        String result = "";
        try {
            int rowCount = 0;
            while (rs.next()) {
                rowCount++;
                if (rowCount == i) {
                    book.setBookId(rs.getString(1));
                    result = book.getBookId();
                    System.out.println(result);
                    break;
                }
            }
        } catch (Exception ignored) {
//            ignored.printStackTrace();
        }
        return result;
    }
}